#include "CPropPicker.h"

#include <vtkRendererCollection.h>

void CPropPickerInteractorStyle::OnLeftButtonDown()
{
    int* clickPos = this->GetInteractor()->GetEventPosition();

    // Pick from this location.
    vtkSmartPointer<vtkPropPicker>  picker =
            vtkSmartPointer<vtkPropPicker>::New();
    picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());

    double* pos = picker->GetPickPosition();
    vtkSmartPointer<vtkSphereSource> sphere=
            vtkSphereSource::New();
    sphere->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper=vtkPolyDataMapper::New();
    mapper->SetInputConnection(sphere->GetOutputPort());
    vtkSmartPointer<vtkActor> actor=vtkActor::New();
    actor->SetMapper(mapper);
    actor->SetPosition(pos);
    actor->GetProperty()->SetColor(0.2,0.4,0.7);
    this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
    // If we picked something before, reset its property
    if (this->LastPickedActor)
    {
        this->LastPickedActor->GetProperty()->DeepCopy(this->LastPickedProperty);
    }
    this->LastPickedActor = picker->GetActor();
    if (this->LastPickedActor)
    {
        // Save the property of the picked actor so that we can restore it next time
        this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
        // Highlight the picked actor by changing its properties
        this->LastPickedActor->GetProperty()->SetColor(0.5, 0.6, 0.7);
        this->LastPickedActor->GetProperty()->SetDiffuse(1.0);
        this->LastPickedActor->GetProperty()->SetSpecular(0.0);
    }

    // Forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}

void CPropPickerInteractorStyle::OnRightButtonDown()
{

}

vtkStandardNewMacro(CPropPickerInteractorStyle);
